package com.limao.servlet;

import javax.servlet.AsyncContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.alibaba.fastjson.JSONException;
import com.limao.CacheManager.AdvInfo;
import com.limao.CacheManager.GoodsInfo;
import com.limao.CacheManager.RegLoginInfo;
import com.limao.CacheManager.SysInfo;
import com.limao.CacheManager.UserInfo;
import com.limao.Model.LzPluginException;
import com.limao.Model.RecvData;
import com.limao.Tool.CacheClass;
import com.limao.Tool.Function;
import com.limao.Utils.OutPrintClass;

public class LoginAsyncProcessor implements Runnable {

	private AsyncContext asyncContext;
	private HttpServletRequest request;
	private HttpServletResponse response;
	private RecvData DataObj=null;

	public LoginAsyncProcessor() {
	}

	public LoginAsyncProcessor(AsyncContext asyncCtx, HttpServletRequest request,HttpServletResponse response,RecvData DataObj) {
		this.asyncContext = asyncCtx;
		this.request = request;
		this.response=response;
		this.DataObj=DataObj;
		
	}

	@Override
	public void run() {

		
		int m = -1;
		Object reply = null;

		try {

			m = Integer.parseInt(DataObj.m);
			
			if(CacheClass.getIntValue(CacheClass.getSYS_BaseDataCacheManager().Load_Param("ispaused"))==1)
			{
				if(CacheClass.Maintenance_Start_time==0) 
					CacheClass.Maintenance_Start_time=CacheClass.getTime();
				
				int timel=CacheClass.getIntValue(CacheClass.getSYS_BaseDataCacheManager().Load_Param("pausedtime"));
				
				OutPrintClass.sendMaintenance(response, asyncContext, m, "系统维护中,维护开始时间："+CacheClass.getFormatDate(CacheClass.Maintenance_Start_time,"yyyy-MM-dd HH:mm:ss")+"，维护时长约"+timel+"分钟");
				return ;
			}
			else
				CacheClass.Maintenance_Start_time=0;
			
			
			switch (m) 
			{
				case Function.GET_SYS_OPERATION_VERSON: // 版本号检查
					reply = SysInfo.resultSysVerson(response,asyncContext,m, DataObj);
					break;
				case Function.REG_IMGCODE: // 取图形验证码
					HttpSession session =request.getSession();
					OutPrintClass.writerNoJson(session,response,asyncContext);
					break;					
				case Function.REG_OPERATION_1: // 手机注册发送验证码
					session =request.getSession();
					reply =RegLoginInfo.getCode(session,response,asyncContext,m, DataObj);
					break;
				case Function.REG_OPERATION_2:// 验证码是否正确 用户名是否已存在
					reply = RegLoginInfo.codeAuth(response,asyncContext,m, DataObj);
					break;
				case Function.REG_OPERATION_3:// 提交注册资料
					reply= RegLoginInfo.AuthInfo(response,asyncContext, m, DataObj);
					break;
				case Function.REG_OPERATION_4:// 提交注册资料(测试用)
					reply= RegLoginInfo.AuthInfo_ForTest(response,asyncContext, m, DataObj);
					break;
				case Function.UPDATA_OPERATION_MOBILE_1:// 修改绑定的手机号(之获取验证码)
				case Function.UPDATA_OPERATION_PASSWORD_1: // 修改密码(获取验证码)
					reply= RegLoginInfo.getCode2( response,asyncContext, m, DataObj);
					break;
				case Function.UPDATA_OPERATION_MOBILE_2:// 修改绑定的手机号(新手机号)
					reply= RegLoginInfo.setNewMobile(response,asyncContext, m, DataObj);
					break;
				case Function.UPDATA_OPERATION_PASSWORD_2:// 修改密码(新密码)
					reply= RegLoginInfo.setNewPassword( response,asyncContext, m, DataObj);
					break;
				case Function.LOGIN_OPERATION_1: //登陆
					
					session = request.getSession();
					String selfSessionId=session.getId(); 
					response= CacheClass.setCookieSession(response, selfSessionId);
					reply= RegLoginInfo.login(session,selfSessionId, response,asyncContext, m, DataObj);
					break;
				case Function.LOGIN_OPERATION_2: //登陆（测试用）
					
					session = request.getSession();
					selfSessionId=session.getId(); 
					response= CacheClass.setCookieSession(response, selfSessionId);
					reply= RegLoginInfo.login_ForTest(session,selfSessionId, response,asyncContext, m, DataObj);
					break;					
			        
				//&新鲜宝   取众筹项目列表信息  1510
				case Function.GET_GOODS_OPERATION_LIST:
					reply =  GoodsInfo.resultSYSGoodsList(response,asyncContext,m, DataObj);
			        break;	
			        
  				//&新鲜宝  取某众筹详细信息（项目主页）  1520
				case Function.GET_GOODS_OPERATION_INFO: 
					reply =  GoodsInfo.resultSYSGoodsINFO(response,asyncContext,m, DataObj);
					break;
					
				case Function.GET_SYS_OPERATION_Params: // 系统参数
					reply = SysInfo.GET_SYS_OPERATION_Params(response,asyncContext,m, DataObj);
					break;
					
				//&新鲜宝  取某众筹详细信息（话题页）  1540
				case Function.GET_GOODS_OPERATION_DISCUSS: 
					reply =  GoodsInfo.resultSYSGoodsDISCUSSINFO(response,asyncContext,m, DataObj);
					break;
					
		  		//&新鲜宝  取某众筹详细信息（支持者页） 1530
				case Function.GET_GOODS_OPERATION_INFO2: 
					reply =  GoodsInfo.resultSYSGoodsBuy(response,asyncContext,m, DataObj);
					break;		
			  	
	  			//&新鲜宝  取支付方式  1548
				case Function.GET_SYS_OPERATION_PAYTYPE: 
					reply =  GoodsInfo.resultSYSPayType(response,asyncContext,m, DataObj);
					break;					

				//新鲜宝 提现说明文字	91				
				case Function.withdraw_Day_NUM:    //提现多少天多少次参数
					reply =  SysInfo.withdraw_Day_NUM(response,asyncContext,m, DataObj);
					break;	
					
				//&新鲜宝 众筹项目种类	95				
				case Function.Goods_Cate_List:    
					reply =  SysInfo.Project_Cate_List(response,asyncContext,m, DataObj);
					break;	
					
				//&新鲜宝	应用的欢迎页广告图片	9008
				case Function.GET_SYS_OPERATION_FACE_IMAGE:
				case Function.GET_HomeMenu: //首页快捷菜单列表
					reply = AdvInfo.resultSYSFaceImage(response,asyncContext,m, DataObj);
					break;	
					

				//&新鲜宝	首页滑动广告列表	 222
				case Function.GET_SCREENADV:
					reply = AdvInfo.resultSysHomeAdv(response,asyncContext,m, DataObj);
					break;	
					
					
				//&新鲜宝	首页推荐内容列表	226
				case Function.GET_HomeContent:
					reply = AdvInfo.GET_HomeContent(response,asyncContext,m, DataObj);
					break;	

				//&新鲜宝	发起众筹之取认证费用系统参数	227
				case Function.GET_CompanyAuthRmbParm:
					reply = AdvInfo.GET_CompanyAuthRmbParm(response,asyncContext,m, DataObj);
					break;						
					
					
					
					
				//&新鲜宝	新手课堂	100
				case Function.Study_List0:
					reply = UserInfo.Study_List0(response,asyncContext,m, DataObj);
					break;						
				//&新鲜宝	公司活动	101
				case Function.Study_List1:
					reply = UserInfo.Study_List1(response,asyncContext,m, DataObj);
					break;	
				//&新鲜宝	详情	105
				case Function.Study_List_DetailInfo:
					reply = UserInfo.Load_SySStudy_ById(response,asyncContext,m, DataObj);
					break;						
				//&新鲜宝	取价格筛选列表	 228
				case Function.GET_PRICELIST:
					reply = AdvInfo.GET_PRICELIST(response,asyncContext,m, DataObj);
					break;						
					
						
				//&新鲜宝 排行榜					
				case Function.LOAD_Rank: 
					String userid=CacheClass.isLogin(request,response);
					if (CacheClass.isEmpty(DataObj.userid))
					{
						DataObj.userid=userid;
					}
					reply = UserInfo.getRankInfo(response,asyncContext,m, DataObj);
				    break;		
						
						
				default:
					reply = OutPrintClass.errorObj(m,"出错，功能不存--L");
		
			}
			

		} catch (NumberFormatException e) {
			reply = OutPrintClass.errorObj(m,  "出错，数据类型出错");
			e.printStackTrace();
		} catch (ClassCastException e) {
			reply = OutPrintClass.errorObj(m, "出错，数据类型出错");
			e.printStackTrace();
		} catch (JSONException e) {
			reply = OutPrintClass.errorObj(m,  "出错，JSON数据错误");
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			reply = OutPrintClass.errorObj(m, "出错，数据项不符合要求");
			e.printStackTrace();
		} catch (LzPluginException e) {
			reply = OutPrintClass.errorObj(m,e.getMessage());
			e.printStackTrace();
		} catch (UnsupportedOperationException e) {
			reply = OutPrintClass.errorObj(m, "出错，数据项为只读");
			e.printStackTrace();
		} catch (Exception e) {
			reply = OutPrintClass.errorObj(m, "出错，未知错误:LoginAsyncProcessor");
			e.printStackTrace();
		}
			
		
		OutPrintClass.writer(response, reply, asyncContext);

	}

	

}
